package com.white.commonlib.engine.img;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.Target;
import com.white.commonlib.R;
import com.white.commonlib.manager.ThreadManager;

import java.io.File;
import java.util.concurrent.ExecutionException;

/**
 * Created by ytf on 2017/3/20 020.
 * Description: 图片加载的通用类
 */

public class DefaultImageLoader implements IImageLoader
{
    protected Context mContext;
    private Handler mHandler;

    public DefaultImageLoader(Context context)
    {
        mContext = context;
        mHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    public void init(){
        
    }

    @Override
    public void displayImage(@NonNull String urlOrPath, @NonNull ImageView iv, @DrawableRes int holder, @DrawableRes int error)
    {
        Glide.with(mContext)
                .load(urlOrPath)
                .error(error)
                .placeholder(holder)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(iv);
    }

    @Override
    public void displayImage(@NonNull File imgFile, @NonNull ImageView iv, @DrawableRes int holder, @DrawableRes int error)
    {
        Glide.with(mContext)
                .load(imgFile)
                .error(error)
                .placeholder(holder)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(iv);
    }

    @Override
    public void displayImage(@NonNull byte[] rawData, @NonNull ImageView iv, @DrawableRes int holder, @DrawableRes int error)
    {
        Glide.with(mContext)
                .load(rawData)
                .error(error)
                .placeholder(holder)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(iv);
    }

    /***
     * 显示一张图片, 暂不支持https
     * @param url   url可以是本地文件绝对路径或者网络地址
     * @param iv    imageview
     * @param cacheEnable    是否支持缓存
     */
    public void showImg(String url, ImageView iv, boolean cacheEnable)
    {
        if(url.startsWith("file:"))
        {
            Glide.with(mContext)
                    .load(new File(url))
                    .error(R.drawable.default_adv)
                    .placeholder(R.drawable.default_adv)
                    .diskCacheStrategy(cacheEnable ? DiskCacheStrategy.RESULT : DiskCacheStrategy.NONE)
                    .into(iv);
        }else
        {
            Glide.with(mContext)
                    .load(url)
                    .error(R.drawable.default_adv)
                    .placeholder(R.drawable.default_adv)
                    .diskCacheStrategy(cacheEnable ? DiskCacheStrategy.RESULT : DiskCacheStrategy.NONE)
                    .into(iv);
        }
    }

    /***
     * 显示一张图片，占位图和加载错误的图片已经设置了默认
     * @param url
     * @param iv
     */
    public void showImg(String url, ImageView iv)
    {
        if(url.startsWith("file:"))
        {
            displayImage(new File(url), iv, R.drawable.default_adv, R.drawable.default_adv);
        }else
        {
            displayImage(url, iv, R.drawable.default_adv, R.drawable.default_adv);
        }
    }

    @Override
    public void decodeImage(@NonNull final String url,final int height, final int width,
                            final boolean cacheEnable,final OnDecodeResultCallback callback)
    {
        ThreadManager.getInstance().get().submit(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    final Bitmap bmp = Glide.with(mContext)
                            .load(url)
                            .asBitmap()
                            .diskCacheStrategy(cacheEnable ? DiskCacheStrategy.RESULT : DiskCacheStrategy.NONE)
                            .into(width, height)
                            .get();
                    mHandler.post(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            callback.onComlete(bmp);
                        }
                    });
                } catch (final InterruptedException e)
                {
                    e.printStackTrace();

                    mHandler.post(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            e.printStackTrace();
                            callback.onFailed(e.getLocalizedMessage());
                        }
                    });
                } catch (final ExecutionException e)
                {
                    e.printStackTrace();
                    mHandler.post(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            e.printStackTrace();
                            callback.onFailed(e.getLocalizedMessage());
                        }
                    });
                }
            }
        });
    }

    /***
     * 按原始尺寸解析一张图片
     * @param url
     * @param callback
     * @return
     */
    public void decodeImage(@NonNull final String url,final boolean cacheEnable,final OnDecodeResultCallback callback)
    {
        ThreadManager.getInstance()
                .get().execute(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    final Bitmap bmp = Glide.with(mContext)
                            .load(url)
                            .asBitmap()
                            .diskCacheStrategy(cacheEnable ? DiskCacheStrategy.RESULT : DiskCacheStrategy.NONE)
                            .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                            .get();
                    callback.onComlete(bmp);
                } catch (final Exception e)
                {
                    e.printStackTrace();

                    mHandler.post(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            e.printStackTrace();
                            callback.onFailed(e.getLocalizedMessage());
                        }
                    });
                }
            }
        });
    }
}
